﻿#include "BrowserTableHandlerNCTUserDefined.h"
#include "OperatorsInterface/OperAbstractNCT.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsSpectraManager.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTUserDefined.h"
#include <QPushButton>

namespace GUI
{
    void BrowserTableHandlerNCTUserDefined::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Absorption Coefficient") << tr("IL-Resonant")
            << tr("IL-NonResonant") << tr("Color") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerNCTUserDefined::updateData()
    {
        if (_tableWidget == nullptr) return;
        //获取物理属性管理器
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return;
        Acoustics::FITKAcousticsNCTManager* ntcmgr = phys->getFITKAcousticsNCTManager();
        if (ntcmgr == nullptr) return;

        QList<Acoustics::FITKAcousticsAbsNCT*>  list = ntcmgr->getAcousticsNCTByType(
            Acoustics::FITKAcousticsAbsNCT::NCTType::UserDefTreatment);
        const int n = list.size();
        _tableWidget->setRowCount(n);
        for (int i = 0; i < n; ++i)
        {
            //转换为自定义声学包
            Acoustics::FITKAcousticsNCTUserDefined* ud = dynamic_cast<Acoustics::FITKAcousticsNCTUserDefined*>
                (list.at(i));
            if (ud == nullptr) continue;
            //数据ID
            const int id = ud->getDataObjectID();
            //填充表格，用type表示ID
            _tableWidget->setItem(i, 0, new QTableWidgetItem(ud->getDataObjectName(), id));
            _tableWidget->setItem(i, 1, new QTableWidgetItem(getSpectra(ud->getAbsorptionCoeffID()) , id));
            _tableWidget->setItem(i, 2, new QTableWidgetItem(getSpectra(ud->getInsertionLossResonantID()), id));
            _tableWidget->setItem(i, 3, new QTableWidgetItem(getSpectra(ud->getInsertionLossNonResonantID()), id));
            float color[3] = { 0,0,0 };
            ud->getColor(color);
            QPushButton* button = new QPushButton(_tableWidget);
            this->SetPushButtonColor(button, color[0] * 255, color[1] * 255, color[2] * 255);
            _tableWidget->setCellWidget(i,4, button);
            connect(button, &QPushButton::clicked, this, [=] {this->onEditItemColor(button, ud); });
            //_tableWidget->setItem(i, 5, new QTableWidgetItem(ud->getDescribe(), id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    QString BrowserTableHandlerNCTUserDefined::getSpectra(int id)
    {
        Acoustics::FITKAcousticsPhysics* phys = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phys == nullptr) return QString();
        Acoustics::FITKAcousticsSpectraManager* spmgr = phys->getFITKAcousticsSpectraManager();
        if (spmgr == nullptr) return QString();
        auto spec = spmgr->getDataByID(id);
        if (spec)
            return spec->getDataObjectName();
        return QString();
    }

    EventOper::OperAbstractNCT * BrowserTableHandlerNCTUserDefined::getNCTOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractNCT>("OperNCTUserDefined");
    }

    bool BrowserTableHandlerNCTUserDefined::enableAttriSet()
    {
        QList<QTableWidgetItem*> selectItems = this->getSelectItems();
        if (selectItems.size() < 2) return false;

        QTableWidgetItem* item = selectItems.at(0);
        if (item == nullptr) return false;
        //名称列
        const int colIndex = item->column();
        //第2到4列能修改
        if (colIndex <1|| colIndex>3) return false;

        return true;
    }
}